package engine.groups
{
	import ash.core.Entity;
	
	import engine.GameEngine;
	import engine.groups.other.CursorGroup;
	import engine.managers.ScreenManager;
	import engine.systems.collision.collision.CollisionSystem;
	import engine.systems.input.interaction.InteractionSystem;
	import engine.systems.motion.motion.MotionSystem;
	import engine.systems.render.particle.ParticleSystem;
	import engine.systems.render.render.Render;
	import engine.systems.render.render.RenderSystem;
	import engine.systems.spatial.edge.EdgeSystem;
	import engine.systems.spatial.sleep.SleepSystem;
	import engine.systems.spatial.spatial.Spatial;
	import engine.systems.spatial.spatial.SpatialSystem;
	import engine.systems.spatial.spatialOffset.SpatialOffsetSystem;
	import engine.systems.timeline.timeline.TimelineSystem;
	import engine.systems.timeline.timelineClip.TimelineClipSystem;
	import engine.systems.ui.screenAlign.BoxAlign;
	import engine.systems.ui.screenAlign.ScreenAlignSystem;
	import engine.systems.update.GameMonitorSystem;
	import engine.systems.update.timer.TimerSystem;
	import engine.systems.update.tween.TweenSystem;
	import engine.utils.DisplayUtils;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.display.Sprite;
	
	import game.utils.GameUtils;

	public class Scene extends DisplayGroup
	{
		private var _overlayContainer:DisplayObjectContainer;
		
		public function Scene(container:DisplayObjectContainer, parent:Group)
		{
			super(container, parent);
			
			this._overlayContainer = new Sprite();
			this._overlayContainer.mouseEnabled = false;
			this._overlayContainer.name = this.name + "Overlay";
			container.addChild(this._overlayContainer);
			
			new CursorGroup(this);
			
			this.setupSystems();
			this.setupBackground();
			
			this.center();
		}
		
		override public function destroy():void
		{
			super.destroy();
			
			DisplayUtils.destroy(this._overlayContainer);
			this._overlayContainer = null;
		}
		
		private function setupSystems():void
		{
			this.addSystem(new RenderSystem());
			this.addSystem(new SpatialSystem());
			this.addSystem(new ParticleSystem());
			this.addSystem(new SpatialOffsetSystem());
			this.addSystem(new EdgeSystem());
			this.addSystem(new MotionSystem());
			this.addSystem(new TweenSystem());
			this.addSystem(new TimerSystem());
			this.addSystem(new SleepSystem());
			this.addSystem(new InteractionSystem());
			this.addSystem(new CollisionSystem());
			this.addSystem(new TimelineSystem());
			this.addSystem(new TimelineClipSystem());
			this.addSystem(new GameMonitorSystem());
		}
		
		private function setupBackground():void
		{
			this.addSystem(new ScreenAlignSystem());
			
			var entity:Entity = new Entity("SleepDisplay");
			this.addEntity(entity);
			
			var render:Render = new Render(GameEngine.screenManager.sleepDisplay);
			render.remove = false;
			entity.add(render);
			
			entity.add(new BoxAlign());
			entity.add(new Spatial());
		}
		
		public function get overlayContainer():DisplayObjectContainer { return this._overlayContainer; }
		
		private function center():void
		{
			var icon:Entity = GameUtils.createLoadingIcon(this, this.overlayContainer);
			var spatial:Spatial = icon.get(Spatial);
			spatial.x = 80;
			spatial.y = 55;
			
			var shape:Shape = new Shape();
			shape.graphics.beginFill(0x333333);
			shape.graphics.drawCircle(0, 0, 10);
			shape.graphics.endFill();
			this.groupContainer.addChild(shape);
		}
	}
}